package com.kongge.viewdisplayframeworklib.landscape;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

import com.kongge.viewdisplayframeworklib.control.PublicInterface;
import com.kongge.viewdisplayframeworklib.event.action.EQAction;
import com.kongge.viewdisplayframeworklib.event.action.EQGotoFrameAction;
import com.kongge.viewdisplayframeworklib.model.AbsUIController;
import com.kongge.viewdisplayframeworklib.model.PageControlStack;
import com.kongge.viewdisplayframeworklib.view.TabWidget;

import java.util.HashMap;
import java.util.List;

public class LandsUIManager extends AbstractUIManager {

	// ~常量区块
	// ========================================================

	// ~成员变量区块
	// ========================================================
	public PageControlStack mStack;
	public int mLinkId = -1;
	public Activity activity;
	public EQAction gotoFrameAction;
	public HashMap<Integer, EQAction> hashMap = new HashMap<Integer, EQAction>();

	// ~构造函数区块
	// ========================================================

	// ~方法区块
	// ========================================================
	public void setActivity(Activity activity) {
		this.activity = activity;
	}
	
	@Override
	public Context getContext() {
		// TODO Auto-generated method stub
		return activity;
	}

	@Override
	public void setTabWidgetHeight(int nHeight) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setParentViewGroup(ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		this.mContentView = viewGroup;
	}

	@Override
	public void setTabWidget(TabWidget tabWidget) {
		// TODO Auto-generated method stub

	}

	@Override
	public void gotoTabSection(int nIndex) {
		// TODO Auto-generated method stub

	}

	@Override
	public void goBack() {
		// TODO Auto-generated method stub
		if (mStack != null) {
			AbsUIController control = mStack.goBack();
			if (control != null) {
				int frameId = control.getPageId();
				hashMap.remove(frameId);
				recalllayoutControl(control);
			}
		}
	}

	@Override
	public void goBackToFirstPage() {
		if (mStack != null) {
			AbsUIController uiController = mStack.getCurFirstPageControl();
			if (uiController != null) {
				int firstPageId = uiController.getPageId();
				if (firstPageId != 0) {
					gotoPageWithStack(mStack, firstPageId);
				}
			}
		}
	}


	@Override
	public AbsUIController getCurPageControl() {
		// TODO Auto-generated method stub
		AbsUIController uiController = mStack.getTopUIControl();
		return uiController;
	}

	@Override
	public AbsUIController getLastPageControl() {
		// TODO Auto-generated method stub
		AbsUIController uiController = null;
		if (mStack != null) {
			List<AbsUIController> uiControllerList = mStack.getPageControlList();
			int count = 0;
			if (uiControllerList != null) {
				count = uiControllerList.size();
				if (count > 1) {
					uiController = uiControllerList.get(count -2);
				}
			}
		}
		return uiController;
	}

	@Override
	public String getCurPageTitle() {
		// TODO Auto-generated method stub
		String title = mStack.getCurPageTitle();
		return title;
	}

	@Override
	public boolean isCanGoBack() {
		// TODO Auto-generated method stub
		boolean isCanGoBack = mStack.canGoBack();
		return isCanGoBack;
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		mStack = new PageControlStack();
	}

	@Override
	public void gotoPage(EQAction gotoFrameAction) {
		// TODO Auto-generated method stub
		int nPageId = ((EQGotoFrameAction)gotoFrameAction).getGotoFrameId();
		hashMap.put(nPageId, gotoFrameAction);
		gotoPageWithStack(getCurUIControlStack(),(EQGotoFrameAction)gotoFrameAction);
	}

	@Override
	public void gotoPage(int nPageId) {
		// TODO Auto-generated method stub
		hashMap.put(nPageId, gotoFrameAction);
		gotoPageWithStack(nPageId);
	}

	@Override
	public PageControlStack getCurUIControlStack() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void recalllayoutControl(AbsUIController control) {
		// TODO Auto-generated method stub
		if (control != null) {
			setContentView(control);
		}
		control.onForeground();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		boolean isDeal = false;
		if (mStack != null) {
			isDeal = mStack.getCurPageControl().onKeyDown(keyCode, event);
			if (isDeal == false) {
				if (KeyEvent.KEYCODE_BACK == keyCode) {
					if (mStack.canGoBack()) {
						PublicInterface.goBack();
						isDeal = true;
					} else {
						Toast toast = Toast.makeText(getContext(), "请转到竖屏返回", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 10);
						toast.show();
						isDeal = true;
					}
					
				}
			}
		}
		
		return isDeal;
	}

	@Override
	public void onDestory() {
		// TODO Auto-generated method stub
		if (mStack != null) {
			List<AbsUIController> uiControllerList = mStack.getPageControlList();
			if (uiControllerList != null) {
				for (int i = 0; i < uiControllerList.size(); i++) {
					AbsUIController uiController = uiControllerList.get(i);
					if (uiController != null) {
						uiController.onRemove();
					}
				}
			}
		}
	}

	@Override
	public void close() {
		// TODO Auto-generated method stub

	}
	
	private void gotoPageWithStack(int nPageId) {
		AbsUIController control = null;
		if (mStack == null) {
			if (nPageId == -1) {
				control = mStack.getTopUIControl();
				if (control == null) {
					nPageId = mLinkId;
				}
			}
			if (control == null) {
				control = mStack.getPageControl(nPageId);
			}
		}
		recalllayoutControl(control);
	}
	
	private void gotoPageWithStack(PageControlStack lastPageStack, EQGotoFrameAction gotoFrameAction) {
		this.gotoFrameAction = gotoFrameAction;
		int nPageId = gotoFrameAction.getGotoFrameId();
		AbsUIController control = null;
		if (mStack != null) {
			if (nPageId == -1) {
				control = mStack.getTopUIControl();
				if (control == null) {
					nPageId = mLinkId;
				}
			}
			
			if (control == null) {
				control = mStack.getPageControl(lastPageStack, gotoFrameAction);
			}
		}
		recalllayoutControl(control);
	}
	
	private void gotoPageWithStack(PageControlStack pageStack, int nPageId) {
		AbsUIController control = null;
		if (pageStack != null) {
			if (nPageId == -1) {
				control = pageStack.getTopUIControl();
				if (control == null) {
					nPageId = mLinkId;
				}
			}
			if (control == null) {
				control = pageStack.getPageControl(nPageId);
			}
		}
		recalllayoutControl(control);
	}

	private void setContentView(AbsUIController control) {
		// TODO Auto-generated method stub
		View rootView = control.getRootView();
		ViewGroup parent = (ViewGroup) rootView.getParent();
		if (parent != null) {
			parent.removeView(rootView);
		}
		if (mContentView != null) {
			mContentView.removeAllViews();
			mContentView.addView(rootView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		}
	}
	
	public EQAction getEqAction(int frameId) {
		EQAction action = null;
		if (hashMap != null) {
			action = hashMap.get(frameId);
		}
		return action;
	}

	@Override
	public void onActivityResume() {
		AbsUIController uiController = getCurPageControl();
		if (uiController != null) {
			uiController.onForeground();
		}
	}

	@Override
	public void onActivityPause() {
		AbsUIController uiController = getCurPageControl();
		if (uiController != null) {
			uiController.onBackground();
		}
	}

	// ~静态方法区块
	// ========================================================

	// ~内部接口（类）区块
	// ========================================================

}
